package com.cdck.safe.dete.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdck.safe.dete.common.result.PageResult;
import com.cdck.safe.dete.common.result.Result;
import com.cdck.safe.dete.common.util.SecurityUtils;
import com.cdck.safe.dete.model.entity.CommunityUser;
import com.cdck.safe.dete.model.query.BaseKeywordsQuery;
import com.cdck.safe.dete.model.query.CommunityUserQuery;
import com.cdck.safe.dete.model.vo.CommunityUserVo;
import com.cdck.safe.dete.service.CommunityUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * (CommunityUser)表控制层
 *
 * @author wp
 * @since 2024-02-26 23:02:29
 */
@Api(value = "communityUser")
@RestController
@RequestMapping("communityUser")
@RequiredArgsConstructor
public class CommunityUserController {

    private final CommunityUserService communityUserService;


    @ApiOperation(value = "分页列表")
    @GetMapping("/page")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "queryParams"  ,value ="查询条件" )
    })
    public PageResult<CommunityUserVo> queryByPage(CommunityUserQuery queryParams) {
        IPage<CommunityUserVo> result = communityUserService.queryByPage(queryParams);
        return PageResult.success(result);

    }

    @ApiOperation(value = "表单数据")
    @GetMapping("/{communityId}/form")
    public Result<CommunityUser> getCommunityUserForm(
           @PathVariable Long communityId
    ) {
        Long userId = SecurityUtils.getUserId();
        CommunityUser formData = communityUserService.getCommunityUserFormData(communityId, userId);
        return Result.success(formData);
    }


    /**
     * 新增数据
     *
     * @param communityUser 实体
     * @return 新增结果
     */
    @ApiOperation(value = "新增")
    @PostMapping
    public Result add(
        @RequestBody @Valid
        CommunityUser communityUser
        ) {
        communityUser.setUserId(SecurityUtils.getUserId());
        boolean result = this.communityUserService.saveCommunityUser(communityUser);
        return Result.judge(result);
    }

    @ApiOperation(value = "修改")
    @PutMapping(value = "/{id}")
    public Result updateCommunityUser(
            @PathVariable Long id,
            @RequestBody @Validated CommunityUser communityUser) {
        boolean result = communityUserService.updateCommunityUser(id, communityUser);
        return Result.judge(result);
    }


    @ApiOperation(value = "删除数据")
    @DeleteMapping("/{ids}")
    public Result deleteCommunityUsers(
             @PathVariable String ids
    ) {
        boolean result = communityUserService.deleteCommunityUsers(ids);
        return Result.judge(result);
    }


    @ApiOperation(value = "修改用户状态")
    @PutMapping(value = "/update/status/{id}")
    public Result updateStatusCommunityUser(
            @PathVariable Long id
           ) {
        boolean result = communityUserService.updateStatusCommunityUser(id);
        return Result.judge(result);
    }

}

